#include "moviecontrol.h"

#include <QFile>
#include <QIODevice>
#include <QDataStream>
#include <QtNetwork/QHttpResponseHeader>
#include "src/db/DbModule.h"



MovieControl::MovieControl(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);
	ui.panelSelector->addItem(QString::fromUtf8("preview"));
	ui.panelSelector->addItem(QString::fromUtf8("source1"));
	ui.panelSelector->addItem(QString::fromUtf8("source2"));
	ui.panelSelector->addItem(QString::fromUtf8("source3"));
	ui.panelSelector->addItem(QString::fromUtf8("source4"));
	ui.panelSelector->addItem(QString::fromUtf8("source5"));

	ui.panelSaveSelector->addItem(QString::fromUtf8("preview"));
	ui.panelSaveSelector->addItem(QString::fromUtf8("source1"));
	ui.panelSaveSelector->addItem(QString::fromUtf8("source2"));
	ui.panelSaveSelector->addItem(QString::fromUtf8("source3"));
	ui.panelSaveSelector->addItem(QString::fromUtf8("source4"));
	ui.panelSaveSelector->addItem(QString::fromUtf8("source5"));
	
	ui.loadBox->addItem(QString::fromUtf8("preview"));
	ui.loadBox->addItem(QString::fromUtf8("source1"));
	ui.loadBox->addItem(QString::fromUtf8("source2"));
	ui.loadBox->addItem(QString::fromUtf8("source3"));
	ui.loadBox->addItem(QString::fromUtf8("source4"));
	ui.loadBox->addItem(QString::fromUtf8("source5"));

	connect( ui.pushButton, SIGNAL( clicked() ) , this, SLOT( test() ) );
	connect( ui.openButton,SIGNAL (clicked()) ,this, SLOT (selectFile()) );
	connect( ui.saveButton,SIGNAL (clicked()) ,this, SLOT (savePosition()) );
	connect( ui.filterButton,SIGNAL (clicked()), this, SLOT (filter()));
	connect(ui.savedList,SIGNAL (clicked(QModelIndex)), this, SLOT (load(QModelIndex)));
}

void MovieControl::setPool(DataPool *_pool) {
	pool = _pool;
}


void MovieControl::load(QModelIndex index) {
	Db *db = (Db*)pool->getIdProvider();
	Texture *t = getSelectedLoadTexture();
	MovieData *data = list->movies->get(index.row());
	data = db->loadMoviePart(data->id->get());
	printf("loaded\n");
	t->filebase->set(data->filename->get());
	t->start->set(data->start->get());
	t->stop->set(data->stop->get());
	t->current->set((float)data->start->get());
	t->playMode->set(1);
}

void MovieControl::filter() {
	ui.savedList->reset();
	std::string filterString = ui.filterEdit->text().toUtf8().data();
	Db *db = (Db*)pool->getIdProvider();
	list =db->listMovieParts(filterString);
	NPropMovieList *plist = new NPropMovieList(list->movies,this);
	ui.savedList->setModel(plist);
	ui.savedList->reset();
}

void MovieControl::savePosition() {
	Texture *t = getSelectedSaveTexture();
	printf(t->filebase->get().data());
	
	printf(" %d %d\n",t->start->get(),t->stop->get());
	Db *db = (Db*)pool->getIdProvider();
	db->saveMoviePart(ui.saveEdit->text().toUtf8().data(),t->filebase->get(),t->start->get(),t->stop->get());
}

void MovieControl::selectFile() {
	QFileDialog dialog(this);
		 dialog.setFileMode(QFileDialog::ExistingFile);
		 
		 if (dialog.exec()) {
			 
			QStringList files = dialog.selectedFiles();
			if (files.size()==1) {
				ui.lineEdit->setText(files.first());
			}
			 
		 }

	
}

void MovieControl::printState(int state) {
	printf("state: %d\n",state);

}

void MovieControl::download() {
	printf("download\n");
		qint64 size = http->bytesAvailable();
		
		QDataStream out(file);
		char *data = new char[(int)size];

		int read =		http->read(data,size);
				
				out << data;
				printf("read: %d \n",read);
		
		
//		file.flush();
//		file.close();
		//printf(data);
		//printf("\n");
	
}

void MovieControl::test() {
	Texture *t = getSelectedTexture();
	t->filebase->set(ui.lineEdit->text().toLatin1().data());
	t->current->set(-1);
}



Texture* MovieControl::getSelectedTexture() {
	return ((Prop<DisplayPanel*>*)(display->getProp(ui.panelSelector->currentText().toLatin1().data())))->get()->text->get();
}

Texture* MovieControl::getSelectedSaveTexture() {
	return ((Prop<DisplayPanel*>*)(display->getProp(ui.panelSaveSelector->currentText().toLatin1().data())))->get()->text->get();
}

Texture* MovieControl::getSelectedLoadTexture() {
	return ((Prop<DisplayPanel*>*)(display->getProp(ui.loadBox->currentText().toLatin1().data())))->get()->text->get();
}

void MovieControl::setDisplay(VideoDisplay *d) {
	display=d;
	
}

MovieControl::~MovieControl()
{

}
